{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 实践2：实现Azure face人脸验证\n",
    "\n",
    "选用Azure Face - Find Similar API，实现人脸对比，具体实现如下：\n",
    "* Create facelist 创建并命名好你的facelist\n",
    "* Add face 添加至少10张face作为对比人脸数据\n",
    "* get查看facelists中的信息\n",
    "* Detect 检测出你需要的人脸并获取faceid\n",
    "* Find similars 返回相似置信度\n",
    "* 提交HTML档，命名为【姓名-fscelist名-学号】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "# 1、create\n",
    "create_facelists_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\" \n",
    "subscription_key = \"55c0341ac733482ab48481e34a15ae09\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "data = {\n",
    "    \"name\": \"sample_list\",\n",
    "    \"userData\": \"User-provided data attached to the face list.\",\n",
    "    \"recognitionModel\": \"recognition_02\"\n",
    "}\n",
    "params = {\n",
    "    # Request parameters    \n",
    "   \n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "    \n",
    "#     'detectionModel': 'detection_01',\n",
    "}\n",
    "\n",
    "r_create = requests.put(create_facelists_url,headers=headers,params=params,json=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2、Add face 1\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://newmedia.nfu.edu.cn/wp-content/uploads/members/Wang_Zijia.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"d814d18a-24b5-41fc-8fe3-5d4702d823f8\"}'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2、Add face 2\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://cdn.duitang.com/uploads/item/201505/18/20150518213328_tes2K.thumb.700_0.jpeg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"7dc1a93c-2933-4f47-862a-cb3a0908eb00\"}'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"bedd76a5-f2f7-4934-b430-8f0c6674cba0\"}'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 3\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://x0.ifengimg.com/res/2020/A237B19D76EBD74E9B2936E446A120908A434ECB_size72_w1080_h864.jpeg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"c7ed021a-c768-430e-b053-2e2b7b0c0dca\"}'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 4\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://b-ssl.duitang.com/uploads/item/201711/06/20171106190958_HsVe3.jpeg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"5fc081b3-49ec-448f-9333-7aea775bf877\"}'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 5\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://img01.cztv.com/201806/29/f21a31a25e2dd12d73f99470a95eaa6a.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"0a1f27af-205c-4e19-9021-37186a115940\"}'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 6\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://1841.img.pp.sohu.com.cn/images/blog/2011/9/7/0/12/u99073404_132fb9ddb5fg215.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"513436de-2593-418f-a9f5-df59e1e47261\"}'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 7\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://img.3dmgame.com/uploads/allimg/160324/0T55224J-3.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"6fb72242-39fe-4146-bd27-1e21b7457d99\"}'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 8\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://p0.qhimgs4.com/t01d4b17ebffcfb4a6d.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"313be45e-82aa-436a-a7a8-f9e18a3ec3cf\"}'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 9\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://photocdn.sohu.com/20120911/Img352816715.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaceId\":\"d7021836-28a3-4c88-aa69-126df72e0cbb\"}'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2、Add face 10\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}/persistedFaces\"\n",
    "\n",
    "img_url = \"http://www.yoka.com/dna/pics/ba1ea5b5/d3ddbedab59dda7aa31.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"chen_test_01\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})\n",
    "# r.status_code\n",
    "r_add_face.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\"\n",
    "\n",
    "data = {\n",
    "    \"faceListId\":\"chen_test_01\",\n",
    "\n",
    "}\n",
    "\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_get_facelist.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"persistedFaces\":[{\"persistedFaceId\":\"7d07245b-ad22-4239-af4e-a3da8f42df78\",\"userData\":null},{\"persistedFaceId\":\"d814d18a-24b5-41fc-8fe3-5d4702d823f8\",\"userData\":null},{\"persistedFaceId\":\"7dc1a93c-2933-4f47-862a-cb3a0908eb00\",\"userData\":null},{\"persistedFaceId\":\"bedd76a5-f2f7-4934-b430-8f0c6674cba0\",\"userData\":null},{\"persistedFaceId\":\"c7ed021a-c768-430e-b053-2e2b7b0c0dca\",\"userData\":null},{\"persistedFaceId\":\"5fc081b3-49ec-448f-9333-7aea775bf877\",\"userData\":null},{\"persistedFaceId\":\"0a1f27af-205c-4e19-9021-37186a115940\",\"userData\":null},{\"persistedFaceId\":\"513436de-2593-418f-a9f5-df59e1e47261\",\"userData\":null},{\"persistedFaceId\":\"6fb72242-39fe-4146-bd27-1e21b7457d99\",\"userData\":null},{\"persistedFaceId\":\"313be45e-82aa-436a-a7a8-f9e18a3ec3cf\",\"userData\":null},{\"persistedFaceId\":\"d7021836-28a3-4c88-aa69-126df72e0cbb\",\"userData\":null}],\"faceListId\":\"chen_test_01\",\"name\":\"sample_list\",\"userData\":\"User-provided data attached to the face list.\"}'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_get_facelist.content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"a3b83ee1-e0ad-4430-af91-d476b8f44784\", \"faceRectangle\": {\"top\": 176, \"left\": 143, \"width\": 244, \"height\": 244}, \"faceAttributes\": {\"smile\": 1.0, \"headPose\": {\"pitch\": -3.9, \"roll\": -10.3, \"yaw\": 11.5}, \"gender\": \"female\", \"age\": 20.0, \"facialHair\": {\"moustache\": 0.0, \"beard\": 0.0, \"sideburns\": 0.0}, \"glasses\": \"NoGlasses\", \"emotion\": {\"anger\": 0.0, \"contempt\": 0.0, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 1.0, \"neutral\": 0.0, \"sadness\": 0.0, \"surprise\": 0.0}, \"blur\": {\"blurLevel\": \"low\", \"value\": 0.0}, \"exposure\": {\"exposureLevel\": \"goodExposure\", \"value\": 0.65}, \"noise\": {\"noiseLevel\": \"low\", \"value\": 0.0}, \"makeup\": {\"eyeMakeup\": true, \"lipMakeup\": true}, \"accessories\": [], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.29, \"invisible\": false, \"hairColor\": [{\"color\": \"blond\", \"confidence\": 1.0}, {\"color\": \"brown\", \"confidence\": 0.69}, {\"color\": \"gray\", \"confidence\": 0.27}, {\"color\": \"red\", \"confidence\": 0.27}, {\"color\": \"other\", \"confidence\": 0.15}, {\"color\": \"black\", \"confidence\": 0.03}]}}}]'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://1841.img.pp.sohu.com.cn/images/blog/2011/9/7/0/12/u99073404_132fb9ddb5fg215.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "json.dumps(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4、Find similars\n",
    "\n",
    "findsimilars_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"a3b83ee1-e0ad-4430-af91-d476b8f44784\",\n",
    "    \"faceListId\":\"chen_test_01\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchPerson\"\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'[{\"persistedFaceId\":\"0a1f27af-205c-4e19-9021-37186a115940\",\"confidence\":1.0}]'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结：\n",
    "**4 steps**\n",
    "\n",
    "1、create:\n",
    "* 云空间-->put方法-->faceListId:chen_test01\n",
    "\n",
    "2、add face:\n",
    "* 数据库列表-->post方法-->faceListId:chen_test_01 (每次只能加一张脸)-->run,return\"persistedFaceId\"-->重复十次加十张脸\n",
    "\n",
    "3、test face id:\n",
    "* 任取一张img_url-->post方法-->人脸检测获得faceId\n",
    "\n",
    "4、find similars:\n",
    "* 取step3所得faceId与数据库列表中所有的人脸进行对比，找出对应人脸的persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
